cap log close 
log using "${logpath}imputation_estimator.log", replace

/*******************************************************************************
imputation_estimator.do

Analyzes district-level data using the imputation estimator
*******************************************************************************/

clear all

qui {
	use "${cleandatapath}dist_data.dta", clear
	drop if dist_code==14 // Dropping Connecticut

	gen crim_month=mofd(crim_date)
	gen civ_month=mofd(civ_date)
	gen PA_month=mofd(PA_date)

	label var tot_pmt "Total Payments in District (million $)"
	label var log_tot_pmt "Total Payments in District (Logs)"
	label var log_rides "Total Rides in District (Logs)"
	label var log_active_firms "Active Firms in District (Logs)"

	matrix results = [0,0,0,0]

	did_imputation tot_pmt dist_code month_date civ_month, ///
		controls(post_crim prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_tot_pmt dist_code month_date civ_month, ///
		controls(post_crim prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]
		
	local labn15 = _b[pre1]-1.5
	local labn1 = _b[pre1]-1
	local labn5 = _b[pre1]-0.5
	local lab0 = _b[pre1]
	local lab5 = _b[pre1]+0.5
	local lab1 = _b[pre1]+1
	local lab15 = _b[pre1]+1.5
}
event_plot, together graph_opt( ///
	xti(Months Since First Civil Enforcement) xlab(-24(6)24) ///
	yti("`: variable label log_tot_pmt'") ///
	ylab(`labn1' "-1.0" `labn5' "-0.5" `lab0' "0" ///
		`lab5' "0.5" `lab1' "1.0", angle(0)) ///
	xline(0, lcolor(gs8) lpattern(dash)) yline(`lab0', lcolor(gs8)) ///
	graphregion(color(white)) bgcolor(white)) ///
	lag_opt(color(navy)) lag_ci_opt(recast(rcap) color(navy navy))
qui graph export ${outpath}Figure_A6c.pdf, ///
	as(pdf) replace // Figure A6c

qui {
	did_imputation ambulance_id dist_code month_date civ_month, ///
		controls(post_crim prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_rides dist_code month_date civ_month, ///
		controls(post_crim prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation tot_pmt dist_code month_date crim_month, ///
		controls(post_civ prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_tot_pmt dist_code month_date crim_month, ///
		controls(post_civ prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]
		
	local labn15 = _b[pre1]-1.5
	local labn1 = _b[pre1]-1
	local labn5 = _b[pre1]-0.5
	local lab0 = _b[pre1]
	local lab5 = _b[pre1]+0.5
	local lab1 = _b[pre1]+1
	local lab15 = _b[pre1]+1.5
}

event_plot, together graph_opt( ///
	xti(Months Since First Criminal Enforcement) xlab(-24(6)24) ///
	yti("`: variable label log_tot_pmt'") ///
	ylab(`labn15' "-1.5" `labn1' "-1.0" `labn5' "-0.5" `lab0' "0" ///
		`lab5' "0.5" `lab1' "1.0" `lab15' "1.5", angle(0)) ///
	xline(0, lcolor(gs8) lpattern(dash)) yline(`lab0', lcolor(gs8)) ///
	graphregion(color(white)) bgcolor(white)) ///
	lag_opt(color(navy)) lag_ci_opt(recast(rcap) color(navy navy))
qui graph export ${outpath}Figure_A6d.pdf, as(pdf) replace // Figure A6d

qui {
	did_imputation ambulance_id dist_code month_date crim_month, ///
		controls(post_civ prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_rides dist_code month_date crim_month, ///
		controls(post_civ prior_auth) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation tot_pmt dist_code month_date PA_month if month_date>=612, ///
		controls(post_civ post_crim) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_tot_pmt dist_code month_date PA_month if month_date>=612, ///
		controls(post_civ post_crim) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]
		
	local labn15 = _b[pre1]-3
	local labn1 = _b[pre1]-2
	local labn5 = _b[pre1]-1
	local lab0 = _b[pre1]
	local lab5 = _b[pre1]+1
}

event_plot, together graph_opt( ///
	xti(Months Since Prior Authorization Implementation) xlab(-24(6)24) ///
	yti("`: variable label log_tot_pmt'") ///
	ylab(`labn15' "-3" `labn1' "-2" ///
		`labn5' "-1" `lab0' "0" `lab5' "1", angle(0)) ///
	xline(0, lcolor(gs8) lpattern(dash)) yline(`lab0', lcolor(gs8)) ///
	graphregion(color(white)) bgcolor(white)) ///
	lag_opt(color(navy)) lag_ci_opt(recast(rcap) color(navy navy))
qui graph export ${outpath}Figure_A6a.pdf, as(pdf) replace // Figure A6a

qui {
	did_imputation ambulance_id dist_code month_date PA_month if month_date>=612, ///
		controls(post_civ post_crim) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_rides dist_code month_date PA_month if month_date>=612, ///
		controls(post_civ post_crim) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation active_firms dist_code month_date PA_month if month_date>=624, ///
		controls(post_civ post_crim) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]

	did_imputation log_active_firms dist_code month_date PA_month if month_date>=624, ///
		controls(post_civ post_crim) ///
		hbal h(0/23) pretrends(24) minn(0) maxit(200)
	lincom _b[tau23]-_b[pre1]
	matrix results = [results \ r(estimate), r(se), 2*(1-normal(abs(r(estimate)/r(se)))), e(N)]
		
	local labn6 = _b[pre1]-0.6
	local labn4 = _b[pre1]-0.4
	local labn2 = _b[pre1]-0.2
	local lab0 = _b[pre1]
	local lab2 = _b[pre1]+0.2
}

event_plot, together graph_opt( ///
	xti(Months Since Prior Authorization Implementation) xlab(-24(6)24) ///
	yti("`: variable label log_active_firms'") ///
	ylab(`labn6' "-0.6" `labn4' "-0.4" ///
		`labn2' "-0.2" `lab0' "0" `lab2' "0.2", angle(0)) ///
	xline(0, lcolor(gs8) lpattern(dash)) yline(`lab0', lcolor(gs8)) ///
	graphregion(color(white)) bgcolor(white)) ///
	lag_opt(color(navy)) lag_ci_opt(recast(rcap) color(navy navy))
qui graph export ${outpath}Figure_A6b.pdf, as(pdf) replace // Figure A6b

qui {
	matrix results2 = results[2...,1...]'
	matrix rownames results2 = Coefficient SE Pval N
	matrix colnames results2 = Payments LogPayments Rides LogRides Payments ///
		LogPayments Rides LogRides Payments ///
		LogPayments Rides LogRides Firms LogFirms
	matrix civ_res = results2[1...,1..4]
	matrix crim_res = results2[1...,5..8]
	matrix PA_res = results2[1...,9...]
	matrix colnames results2 = Payments CivPayments Rides CivRides Payments ///
		CrimPayments Rides CrimRides Payments LogPayments ///
		Rides LogRides Firms LogFirms
	matrix civcrim_res = (results2[1...,2], results2[1...,4], ///
		results2[1...,6], results2[1...,8]) 

	esttab matrix(PA_res) using ${outpath}Table_A6.tex, ///
		replace label se frag booktabs // Table A6
	esttab matrix(civcrim_res) using ${outpath}Table_A7.tex, ///
		replace label se frag booktabs // Table A7
}

log close